<?php

class getKeyword extends Controller
{

	public function __construct()
	{
		parent::__construct();		
	}
	
	public function run()
	{
		$search_keyword  = $_GET['product_keyword'];
		$totalRd  = $this->getTotalRd();
		
		if($search_keyword=="" || $search_keyword == "SEARCH A PRODUCT"){
			$str = "$totalRd ITEMS FOUND";
		}else{
			$str = "$totalRd ITEMS FOUND FOR <strong>\"$search_keyword\"</strong>";
		}
		
		echo $str;
	}
	
	public function getTotalRd()
	{
		$search_keyword  = ucfirst(trim($_GET['product_keyword']));
		$search_category = $_GET['ads_category'];
		$search_brand	 = $_GET['ads_brand'];
		$search_colour	 = $_GET['ads_colour'];
		$search_size	 = $_GET['ads_size'];
		$search_price	 = $_GET['ads_price'];
		
		$this->import('Database');
		
		$condition = " where published=1 ";
		
		if ($search_keyword == "" || $search_keyword == "SEARCH A PRODUCT")
		{
			
		}
		else
		{
			// Product Keyword
			if (isset($search_keyword) && $search_keyword)
			{
				preg_match_all('/"[^"]+"|[\+\-]?[^ ]+\*?/', $search_keyword, $arrChunks);
			}
			
			$arrValues = array();
			if (isset($arrChunks[0]) && is_array($arrChunks[0]) && $arrChunks[0])
			{
				$condition .= " And (";
				$index = 0;
				foreach ($arrChunks[0] as $strChunks)
				{
					$session_cond .= (($index > 0) ? " OR " : "") . "`name` LIKE '%" . $strChunks . "%' 
						OR  `catalog_colour`.`title` LIKE '%" . $strChunks . "%'  OR `catalog_tags`.`tag` LIKE '%" . $strChunks . "%'";
					$condition .= (($index > 0) ? " OR " : "") . "`name` LIKE ? 
						OR  `catalog_colour`.`title` LIKE ?  OR `catalog_tags`.`tag` LIKE ?";
						
					$arrValues[] = "%" . $strChunks . "%";
					$arrValues[] = "%" . $strChunks . "%";
					$arrValues[] = "%" . $strChunks . "%";
					//$arrValues[] = "%" . $strChunks . "%";
					
					$index++;
				}
				$condition .= ") ";
			}
		}
		
		/*if($search_keyword != ""){
			$condition .= " And (
				`name` LIKE '%".$search_keyword."%' 
				OR `description` LIKE '%".$search_keyword."%' OR `catalog_tags`.`tag` LIKE '%".$search_keyword."%') ";
		}*/

		// Category Or Subcategory
		if($search_category != "all"){
			$countRd 		= $this->Database->execute("SELECT pid FROM tl_taxonomy where id=".$search_category); 
			$countRd->next();
			if($countRd->pid==0){
				$condition .= " And category IN ( SELECT id FROM `tl_taxonomy` WHERE pid =".$search_category.") ";
			}else{
				$condition .= " And category=".$search_category;
			}
		}		

		// Brand
		if($search_brand != "all") $condition .= " And brand=".$search_brand;

		// Colour
		if($search_colour != "all"){
			$condition .= " And `tl_catalog_attribute`.`id_color` = '" . $search_colour . "'";
		}
		
		// Size
		if($search_size != "all"){
			$condition .= " And `tl_catalog_attribute`.`id_size` = '" . $search_size . "'";
		}		
		
		// Price		
		if($search_price != "all"){
			switch($search_price){
				case 1:
					$condition .= " And (price>50 and price<=200) ";
					break;
				case 2:
					$condition .= " And price>200 ";
					break;
				default:
					$condition .= " And price<=50 ";
					break;
			}
		}				

		$sql = "SELECT `product_catalog`.`id` 
							FROM `product_catalog` 
							LEFT JOIN `catalog_tags` ON `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, '') OR `product_catalog`.`tags` LIKE CONCAT('', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE `catalog_tags`.`id` 
							LEFT JOIN `tl_catalog_attribute` ON `tl_catalog_attribute`.`id_catalog`=product_catalog.`id`
                            JOIN `catalog_colour` ON `catalog_colour`.`id`=`tl_catalog_attribute`.`id_color`
							JOIN `catalog_size` ON `catalog_size`.`id`=`tl_catalog_attribute`.`id_size`
							$condition 
							GROUP BY `tl_catalog_attribute`.`id_catalog`";
		$countRd = $this->Database->prepare($sql)
								->execute($arrValues, time(), time()); 
		
		if ($countRd->numRows > 0)
		{
			$totalRd = $countRd->numRows;			
		}
		else
		{
			$totalRd = 0;
		}
		$totalRd = ($totalRd==0) ? "NO" : $totalRd;
		
		return $totalRd;
	}

}

$objGetRd = new getKeyword();
$objGetRd->run();

?>